package com.atts.baccarat.table;

import com.atts.baccarat.table.impl.BigEyeRoad;
import com.atts.baccarat.table.impl.BigRoad;
import com.atts.baccarat.table.impl.CockroachRoad;
import com.atts.baccarat.table.impl.MarkerRoad;
import com.atts.baccarat.table.impl.NextRoad;
import com.atts.baccarat.table.impl.SmallRoad;

/**
 * 百家乐纪录台面
 * 
 * @author Administrator
 * 
 */
public class Dashboard {

	private final BigRoad bigRoad = new BigRoad();
	private final BigEyeRoad bigEyeRoad = new BigEyeRoad();
	private final SmallRoad smallRoad = new SmallRoad();
	private final CockroachRoad cockroachRoad = new CockroachRoad();
	private final MarkerRoad markerRoad = new MarkerRoad();
	private final NextRoad nextRoad = new NextRoad(this.bigRoad);

	/**
	 * 纪录结果，并计算下一次的预告
	 * 
	 * @param color
	 *            RED, 庄赢，　BLUE,闲赢, GREEN，和
	 */
	public void addResult(final TableItemColor color) {

		// 和时， 只更新珠盘路
		if (color != TableItemColor.GREEN) {

			// 大路
			final TableItem bigRoadItem = new TableItem(color,
					TableItemShape.BIG_ROAD);
			this.bigRoad.add(bigRoadItem);

			int nextRoadColumn = 0;
			if (color == TableItemColor.BLUE) {
				nextRoadColumn = 1;
			}

			// 大眼仔
			if (this.nextRoad.getItem(0, nextRoadColumn) != null) {
				this.bigEyeRoad.add(this.nextRoad.getItem(0, nextRoadColumn));
			}

			// 小路
			if (this.nextRoad.getItem(1, nextRoadColumn) != null) {
				this.smallRoad.add(this.nextRoad.getItem(1, nextRoadColumn));
			}

			// 甲由路
			if (this.nextRoad.getItem(2, nextRoadColumn) != null) {
				this.cockroachRoad
				.add(this.nextRoad.getItem(2, nextRoadColumn));
			}

			this.nextRoad.calculate();
		}

		// 珠盘路
		final TableItem markerRoadItem = new TableItem(color,
				TableItemShape.MARKER_ROAD);
		this.markerRoad.add(markerRoadItem);
	}

	/**
	 * 得到显示结果
	 * 
	 * @return　DisplayDashboard
	 */
	public DisplayDashboard getDisplayResult() {
		final DisplayDashboard result = new DisplayDashboard();

		result.setBigRoad(this.bigRoad.getMatrixForDisplay());
		result.setBigEyeRoad(this.bigEyeRoad.getMatrixForDisplay());
		result.setSmallRoad(this.smallRoad.getMatrixForDisplay());
		result.setCockroachRoad(this.cockroachRoad.getMatrixForDisplay());
		result.setMarkerRoad(this.markerRoad.getMatrixForDisplay());
		result.setNextRoad(this.nextRoad.getMatrixForDisplay());

		return result;

	}

	/**
	 * 清空所有
	 */
	public void reset() {
		this.bigRoad.clear();
		this.bigEyeRoad.clear();
		this.smallRoad.clear();
		this.cockroachRoad.clear();
		this.markerRoad.clear();
		this.nextRoad.clear();
	}

	/**
	 * 回退到上一步
	 */
	public void previous() {

		// 和时
		if (!this.isDogfall()) {

			this.bigRoad.previous();
			this.bigEyeRoad.previous();
			this.smallRoad.previous();
			this.cockroachRoad.previous();

			this.nextRoad.calculate();
		}

		this.markerRoad.previous();

	}

	/**
	 * 判断上一次是否和
	 * 
	 * @return boolean
	 */
	private boolean isDogfall() {
		final TableItem last = this.markerRoad.getLast();

		if (last != null && last.getColor() == TableItemColor.GREEN) {
			return true;
		}

		return false;
	}

}
